/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osmf.elements
{
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.engine.FontPosture;
	import flash.text.engine.Kerning;
	
	import flashx.textLayout.elements.Configuration;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.formats.TextAlign;
	import flashx.textLayout.formats.TextLayoutFormat;
	
	import multimedia.util.HTMLUtil;
	
	import mx.containers.Canvas;
	import mx.controls.Label;
	import mx.controls.Text;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.effects.easing.Back;
	
	import org.osmf.media.MediaElement;
	import org.osmf.traits.MediaTraitType;
	import org.osmf.traits.TextDisplayObjectTrait;

	/**
	 * TextElements extends the OSMF class <code>MediaElement</code> for
	 * Texts
	 */
	public class TextElement extends MediaElement
	{

		/**
		 * the DisplayObjectTrait of this Element
		 */
		private var _displayObjectTrait:TextDisplayObjectTrait;
		/**
		 * the text to be shown
		 */
		private var _text:String;
		/**
		 * whether the text contains HTML-Tags for formatting or not
		 */
		private var _formated:Boolean;
		/**
		 * the width of the textField
		 */
		private var _width:Number;
		/**
		 * the height of the textField
		 */
		private var _height:Number;
		/**
		 * the TextAlign of the text in the textfield
		 */
		private var _align:String;
		/**
		 * the font size to use
		 */
		private var _fontSize:Number;
	
		private var _backgroundColor:uint;
		private var _background:Boolean;
		
		/**
		 * constructor
		 * 
		 * @param text the text to be shown
		 * @param formated whether the text contains HTML-Tags for formatting or not
		 * @param align the TextAlign of the text in the textfield, default is TextFormatAlign.LEFT
		 * @param height the height of the textField, default is 0
		 * @param width the width of the textField, default is 0
		 * @param fontSize the size of the font, this is normally detected automatically according to given width and height
		 */
		public function TextElement(text:String=null, 
									formated:Boolean = false, 
									align:String = TextFormatAlign.LEFT, 
									height:Number = 0, 
									width:Number = 0,
									fontSize:Number = 0,
									backgroundColor:uint = 0xffffff,
									background:Boolean = false)
		{
			super();

			this._formated = formated;
			this._align = align;
			this._height = height;
			this._width = width;
			this._fontSize = fontSize;
			this._background = background;
			this._backgroundColor = backgroundColor;
			this.text = text;
		}
		
		/**
		 * getter method for font size
		 */
		public function get fontSize():Number
		{
			return _fontSize;
		}
		
		/**
		 * getter method for width
		 */
		public function get width():Number
		{
			return _width;
		}
		
		/**
		 * getter method for height
		 */
		public function get height():Number
		{
			return _height;
		}

		/**
		 * setter method for the text
		 */
		public function set text(value:String):void
		{
			if (value != text)
			{
				_text = value;

				updateText();
			}
		}

		/**
		 * getter method for the text
		 */
		public function get text():String
		{
			return _text;
		}

		// Internals
		//

		/**
		 * updating the text, setting formating options
		 * and determining the most fitting font size for 
		 * given textField size
		 */
		private function updateText():void
		{
			var textField:TextField = new TextField();
			textField.width = _width;
			textField.height = _height;
			textField.autoSize = TextFieldAutoSize.LEFT;

			var format:TextFormat = new TextFormat();
			format.font = "Verdana";
			format.size = 20;

			textField.defaultTextFormat = format;
			textField.wordWrap = true;
			textField.multiline = true;

			/*var myfilters:Array = [];
			var dropShadow:DropShadowFilter = new DropShadowFilter();
			dropShadow.distance = 0;
			dropShadow.angle = 25;
			dropShadow.alpha = 0.7;
			dropShadow.blurX = 3;
			dropShadow.blurY = 3;
			dropShadow.strength = 2;
			dropShadow.quality = 3;

			myfilters.push(dropShadow);*/


			//textField.filters = myfilters;
			if (text != null)
			{
				if (_formated)
				{
					textField.htmlText = HTMLUtil.entitiesConvert(text);
				}
				else
				{
					textField.text = HTMLUtil.entitiesConvert(text);

				}
				if(_fontSize == 0)
				{
					//fit font size to textField Height
					var oldSize:Object;
					while (textField.textHeight < _height - 80 && format.size <= 60)
					{
						oldSize = format.size;
						format.size += 1;
						textField.setTextFormat(format);
						textField.width = _width;
						textField.height = _height;
					}
					if(textField.textHeight > _height)
					{
						format.size = oldSize;
						textField.setTextFormat(format);
						textField.width = _width;
						textField.height = _height;
					}
				} else
				{
					format.size = _fontSize;
					textField.setTextFormat(format);
					textField.width = _width;
					textField.height = _height;
				}
				textField.background = _background;
				textField.backgroundColor = _backgroundColor;
				
				if (_displayObjectTrait == null)
				{

					_displayObjectTrait = new TextDisplayObjectTrait(textField);

					addTrait(MediaTraitType.DISPLAY_OBJECT, _displayObjectTrait);
				}

				_displayObjectTrait.setSize(textField.width, textField.height);

			}
		}
	}
}